package service

import (
	"github.com/gorilla/websocket"
	"github.com/vmihailenco/msgpack/v5"
	"log"
)

func SendMsg[T any](conn *websocket.Conn, msg T) error {
	data, err := msgpack.Marshal(msg)
	if err != nil {
		log.Println("MessagePack Marshal Error:", err)
		return err
	}
	return conn.WriteMessage(websocket.BinaryMessage, data)
}

func ReceiveMsg[T any](conn *websocket.Conn) (T, error) {
	var msg T
	_, data, err := conn.ReadMessage()
	if err != nil {
		return msg, err
	}
	err = msgpack.Unmarshal(data, &msg)
	return msg, err
}
